package com.example.app.controller;

import com.example.app.config.AuthConfig;
import com.example.app.entity.PayOrderReq;
import com.example.app.entity.RefundReq;
import com.example.app.entity.ReqData;
import com.example.app.service.AuthService;
import com.example.app.util.HttpClient;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class PayController {
    private static final Logger LOG = LoggerFactory.getLogger(PayController.class);
    @Autowired
    private AuthService authService;
    @Autowired
    private AuthConfig authConfig;

    /**
     * 主动扫码
     * @param payOrderReq
     * @return
     */
    @PostMapping("payOrder")
    private Object payOrder(@RequestBody PayOrderReq payOrderReq) {

        ReqData sign = authService.getSign(payOrderReq);
        String json = JSONObject.fromObject(sign).toString();
        LOG.info(json);
        String s = HttpClient.postClient(authConfig.getAuthRest() + "payOrder", json);
        JSONObject jsonObject = JSONObject.fromObject(s);
        return jsonObject;
    }
    /**
     * 被动扫码
     * @param payOrderReq
     * @return
     */
    @PostMapping("createOrder")
    private Object createOrder(@RequestBody PayOrderReq payOrderReq) {

        ReqData sign = authService.getSign(payOrderReq);
        String json = JSONObject.fromObject(sign).toString();
        LOG.info(json);
        String s = HttpClient.postClient(authConfig.getAuthRest() + "createOrder", json);
        JSONObject jsonObject = JSONObject.fromObject(s);
        return jsonObject;
    }
    /**
     * 支付查询
     */
    @PostMapping("bill")
    private Object bill(String billDate ) {

        ReqData sign = authService.getSign(new HashMap <String, String>(){{
                put("billDate",billDate);
            }});
        String json = JSONObject.fromObject(sign).toString();
        LOG.info(json);
        String s = HttpClient.postClient(authConfig.getAuthRest() + "bill", json);
        JSONObject jsonObject = JSONObject.fromObject(s);
        return jsonObject;
    }
    /**
     * 退款
     */
    @PostMapping("refund")
    public Object refund(RefundReq refundNo){
        ReqData sign = authService.getSign(refundNo);
        String json = JSONObject.fromObject(sign).toString();
        LOG.info(json);
        String s = HttpClient.postClient(authConfig.getAuthRest() + "refund", json);
        JSONObject jsonObject = JSONObject.fromObject(s);
        return jsonObject;
    }
    /**
     * 退款查询
     */
    @PostMapping("refundQuery")
    public Object refundQuery(String refundNo){
        ReqData sign = authService.getSign(new HashMap <String, String>(){{
            put("refundNo",refundNo);
        }});
        String json = JSONObject.fromObject(sign).toString();
        LOG.info(json);
        String s = HttpClient.postClient(authConfig.getAuthRest() + "refundQuery", json);
        JSONObject jsonObject = JSONObject.fromObject(s);
        return jsonObject;
    }
}
